#ifndef __CMATCH_H__
#define __CMATCH_H__

#include "cocos2d.h"
#include "Box2D/Box2D.h"
#include "CDebugDrawGLES.h"
#include "CPhysicsSprite.h"
#include "QueryCallback.h"

#include "Commons.h"

USING_NS_CC;
// This is called when a joint in the world is implicitly destroyed
// because an attached body is destroyed. This gives us a chance to
// nullify the mouse joint.
class CMatch;
class DestructionListener : public b2DestructionListener
{
public:
    void SayGoodbye(b2Fixture* fixture) { B2_NOT_USED(fixture); }
    void SayGoodbye(b2Joint* joint);

    CMatch* _match;
};

class CMatch : public b2ContactListener
{
public:
	CMatch();
	~CMatch();

	void initPhysics();

	void draw();
	void update(float dt);

	void addRect(CCNode* parent, CCPoint p);

	bool MouseDown(const b2Vec2& p);
    void MouseUp(const b2Vec2& p);
    void MouseMove(const b2Vec2& p);

	// Let derived tests know that a joint was destroyed.
    void JointDestroyed(b2Joint* joint) { B2_NOT_USED(joint); }

    // Callbacks for derived classes.
    void BeginContact(b2Contact* contact) { B2_NOT_USED(contact); }
    void EndContact(b2Contact* contact) { B2_NOT_USED(contact); }
    void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
    void PostSolve(const b2Contact* contact, const b2ContactImpulse* impulse)
    {
        B2_NOT_USED(contact);
        B2_NOT_USED(impulse);
    }

public:
    friend class DestructionListener;
    friend class BoundaryListener;
    friend class ContactListener;

	CDebugDrawGLES* _debugDraw;
	b2World* _world;
    b2MouseJoint* _mouseJoint;
	b2Vec2 _mouseWorld;
	b2Body* _groundBody;

	CCTexture2D* m_pSpriteTexture;    // weak ref
};

#endif //end __CMATCH_H__